<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'deal-form',
	'enableAjaxValidation'=>false,
)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'company_id'); ?>
		<?php echo $form->dropDownList(
			$model,
			'company_id', 
			CHtml::listData($companies, 'id', 'company_name'),
			array('prompt'=>' - Company -'));
			//Yii::app()->user->defineCityByIp(), 
		?>
		<?php echo $form->error($model,'user_id'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'deal_category_id'); ?>
		<?php echo $form->dropDownList(
			$model,
			'deal_category_id', 
			CHtml::listData(DealCategory::getAllCategories(), 'id', 'name'),
			array('prompt'=>' - Category -'));
			//Yii::app()->user->defineCityByIp(), 
		?>
		<?php echo $form->error($model,'deal_category_id'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'title'); ?>
		<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
		<?php echo $form->error($model,'title'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'discount'); ?>
		<?php echo $form->textField($model,'discount'); ?>
		<?php echo $form->error($model,'discount'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'short_description'); ?>
		<?php echo $form->textArea($model,'short_description',array('rows'=>6, 'cols'=>50)); ?>
		<?php echo $form->error($model,'short_description'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'description'); ?>
		<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
		<?php echo $form->error($model,'description'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'start_date'); ?>
		<?php 
			$this->widget('zii.widgets.jui.CJuiDatePicker', array(
			    'model'=>$model,
				'attribute'=>'start_date',
			    'options'=>array(
			    	'showAnim'=>'fold',
					'dateFormat'=>'yy-mm-dd',
					'changeMonth'=>true,
					'changeYear'=>true,
				),
			    'htmlOptions'=>array(
			    	'value'=> $model->start_date,
					'readonly' => "readonly"
				),
				'language' => 'ru'
			 ));
		?>
		<?php echo $form->error($model,'start_date'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'end_date'); ?>
		<?php 
			$this->widget('zii.widgets.jui.CJuiDatePicker', array(
			    'model'=>$model,
				'attribute'=>'end_date',
			    'options'=>array(
			    	'showAnim'=>'fold',
					'dateFormat'=>'yy-mm-dd',
					'changeMonth'=>true,
					'changeYear'=>true,
				),
			    'htmlOptions'=>array(
			    	'value'=> $model->end_date,
					'readonly' => "readonly"
				),
				'language' => 'ru'
			 ));
		?>
		<?php echo $form->error($model,'end_date'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'price'); ?>
		<?php echo $form->textField($model,'price'); ?>
		<?php echo $form->error($model,'price'); ?>
	</div>

	<?php if($model->isNewRecord): ?>
		Для добавление фотографий необходимо сохранить акцию
	<?php else: ?>
		<?php if($imgs = Img::getImgsByDealId($model->id)): ?>
			<?php echo $this->renderPartial('/deal/_images', array('imgs' => $imgs)); ?>
		<?php endif;?>
	
		<?php echo CHtml::button('Upload image', array('id'=>'upload_img_button', 'onclick'=>'$("#upload_deal_image").dialog("open");')); ?>
	<?php endif; ?>
	
	<?php //echo CHtml::hiddenField('save_type', '', array('id'=>'save_type_button'))?>

	<div class="row buttons">
		<?php echo CHtml::submitButton('Save', array()); ?>
		<?php //echo CHtml::submitButton('Save and Continue', array('onclick'=>'setSaveType(1);')); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<?php 
	$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
		'id'=>'upload_deal_image',
		'options'=>array(
			'title'=>'Upload New Image',
			'autoOpen'=>false,
			'width'=>'500px',
		),
	));
?>

	<?php echo $this->renderPartial('/deal/_image_form', array('model' => new AjaxUploadImageForm , 'dealId'=>$model->id)); ?>
 
<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>

<script type='text/javascript'>
	/* <![CDATA[ */
	
	function setSaveType(type){
		$('#save_type_button').val(type);
		return false;	
	}

	function deleteImg(id) {
		if(confirm('Вы уверены?')) {
			jQuery.ajax({
				'dataType':'json',
				'success':function(transport){ successDelete(transport) },
				'type':'POST',
				'url':'/admin/deal/deleteimg',
				'cache':false,
				'data': 'i='+id
			});
		}		
	}

	function successDelete(transport) {
		if(transport){
			$("#deal_img_"+transport).remove();
		}
		
	}
	
	/* ]]> */
</script>